<template>
<vs-modal :show.sync="show" effect="fade" width="800px">
  <div class="modal-header" slot="modal-header">
    <h4 class="modal-title">{{$t('common.detail')}}</h4>
  </div>  
  <div class="modal-body" slot="modal-body">
    <validator name="form">
      <form class="form-horizontal">
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.name')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.name">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.vendor')}}</label>
          <div class="col-sm-8">
            <select class="form-control" v-model="cfg.vendor">
              <option v-for="option in vendorOptions" :value="option.value">{{option.text}}</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.ipaddr')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.ipaddr" v-validate:ipaddr="{ip: true}">
            <strong class="text-danger my-error" v-if="$form.ipaddr.ip">{{$t('error.invalidAddr')}}</strong>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.port')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.port" number v-validate:port="{port: true}">
            <strong class="text-danger my-error" v-if="$form.port.port">{{$t('error.invalidPort')}}</strong>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.username')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.username">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.password')}}</label>
          <div class="col-sm-8">
            <input type="password" class="form-control" v-model="cfg.password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.pushNum')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.pushNum" number>
          </div>
        </div>  
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.topoIdOfMediaDev')}}</label>
          <div class="col-sm-8">
            <select class="form-control" v-model="cfg.topoIdOfMediaDev" :disabled="cfg._id > 0">
              <option v-for="option in topoDevsOptions" :value="option.value">{{option.text}}</option>
            </select>
          </div>
        </div>           
      </form>  
    </validator>
  </div>
  <div class="modal-footer" slot="modal-footer">
    <button type="button" class="btn btn-default" @click.prevent="onQuit">{{$t('common.quit')}}</button>
    <button type="button" class="btn btn-primary" @click.prevent="onSaveChanges">{{$t('common.savechanges')}}</button>
  </div>    
</vs-modal>  
</template>

<script>
import _ from 'lodash'
import ebus from 'libs/ebus.js'
import toast from 'libs/toast.js'
import locales from '../locales/monitorPlat.js'
import {printObj, copyObj} from 'libs/utils.js'
import * as actions from 'share/server/vuex/actions/media.js'
import {mediaApi} from 'share/server/api.js'

export default {
  locales,
  vuex: {
    getters: {
      topoDevs: (state) => state.media.topoDevs.list
    },
    actions
  },
  computed: {
    topoDevsOptions () {
      let onlineStr = this.$t('brackets', {content: this.$t('common.online')})
      var options = _.map(this.topoDevs, (item) => {
        return {
          text: item.devName + (1 === item.online ? onlineStr : ''),
          value: item.topoId
        }
      })

      options.splice(0, 0, {
        text: this.$t('common.autosel'),
        value: -1
      })

      return options
    }
  },
  data () {
    return {
      show: false,
      cfg: {
        _id: -1,
        name: '',
        ipaddr: '',
        port: 0,
        username: '',
        password: '',
        pushNum: 0,
        topoIdOfMediaDev: -1,
        vendor: 'standard'
      },
      vendorOptions: [{
        value: 'standard',
        text: this.$t('vendor.standard')
      }, {
        value: 'haige',
        text: this.$t('vendor.haige')
      }, {
        value: 'keda',
        text: this.$t('vendor.keda')
      }]
    }
  },
  methods: {
    onQuit () {
      this.show = false
    },
    onSaveChanges () {
      if (this.$form.invalid) {
        toast.w(this, this.$t('common.fail'), this.$t('error.params'))
        return
      }

      mediaApi.setItem('monitors', this.cfg).then(function(res) {
        if (res && res.data && res.data.code === 0) {
          this.doLoadMonitors()
          ebus.emit('load-mt-srcs')
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
      this.show = false
    },
    onShow (val) {
      copyObj(this.cfg, val)
      this.show = true
    }
  },
  ready () {
    ebus.on('show-monitor-detail', this.onShow)
  }
}
</script>

<style>
</style>